#include "modifymemberinformation.h"
#include "ui_modifymemberinformation.h"
#include "inquiresql.h"
#include <QMessageBox>
#include <QKeyEvent>

ModifyMemberInformation::ModifyMemberInformation(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModifyMemberInformation)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(375, 385);
    setWindowTitle(tr("修改会员信息"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    m_lastClick = -1;

//    QRegExp regExpEmail(tr("^[(a-z,A-Z,0-9)-]+(.[(a-z,A-Z,0-9)-]+)*@[(a-z,A-Z,0-9)-]+(.[(a-z,A-Z,0-9)-]+)+$"));
//    QRegExpValidator *pRegExpValidatorEmail = new QRegExpValidator(regExpEmail, this);
//    ui->lineEditEmail->setValidator(pRegExpValidatorEmail);

    //添加时间过滤器
    ui->lineEditMembercardID->installEventFilter(this);
    ui->lineEditMemberName->installEventFilter(this);
    ui->comboBoxGender->installEventFilter(this);
    ui->lineEditTel->installEventFilter(this);
    ui->lineEditEmail->installEventFilter(this);
    ui->lineEditAdress->installEventFilter(this);
    ui->textEditRemark->installEventFilter(this);
    ui->pushButtonSave->installEventFilter(this);
}

ModifyMemberInformation::~ModifyMemberInformation()
{
    delete ui;
}

PersonMember m_personMember;
void ModifyMemberInformation::displayMemberInformation(const QString &MembercardID)
{
//    PersonMember m_personMember = m_sql->InquireMemberInformation(MembercardID);
    m_personMember = m_sql->InquireMemberInformation(MembercardID);
    ui->lineEditMembercardID->setText(m_personMember.get_personID());
    ui->lineEditMemberName->setText(m_personMember.get_personName());
    if (m_personMember.get_personGender() == QString(tr("男性")))
    {
//        qDebug() << "男性";
        ui->comboBoxGender->setCurrentIndex(0);
    }
    else
        ui->comboBoxGender->setCurrentIndex(1);
    ui->lineEditTel->setText(m_personMember.get_personTel());
    ui->lineEditEmail->setText(m_personMember.get_personEmail());
    ui->lineEditAdress->setText(m_personMember.get_personAdress());
    ui->textEditRemark->setText(m_personMember.get_memberRemark());
}

bool ModifyMemberInformation::eventFilter(QObject *target, QEvent *event)
{
    if (target == ui->lineEditMembercardID || target == ui->lineEditMemberName
            || target == ui->comboBoxGender || target == ui->lineEditTel
            || target == ui->lineEditEmail || target == ui->lineEditAdress
            || target == ui->textEditRemark || target == ui->pushButtonSave)
    {
        //鼠标左键按下事件
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                if (target == ui->lineEditMembercardID)
                {
                    if (m_lastClick != lineEditMembercardID)
                    {
                        ui->lineEditMembercardID->selectAll();
                        m_lastClick = lineEditMembercardID;
                        return true;
                    }
                    else if (m_lastClick == lineEditMembercardID)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditMemberName)
                {
                    if (m_lastClick != lineEditMemberName){
                        ui->lineEditMemberName->selectAll();
                        m_lastClick = lineEditMemberName;
                        return true;
                    }else if (m_lastClick == lineEditMemberName)
                    {
                        goto flag;
                    }
                }
                if (target == ui->comboBoxGender)
                {
                    ui->comboBoxGender->setCurrentIndex(-1);
                }
                if (target == ui->lineEditTel)
                {
                    if (m_lastClick != lineEditTel){
                        m_lastClick = lineEditTel;
                        ui->lineEditTel->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditTel)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditEmail)
                {
                    if (m_lastClick != lineEditEmail){
                        m_lastClick = lineEditEmail;
                        ui->lineEditEmail->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditEmail)
                    {
                        goto flag;
                    }
                }
                if (target == ui->lineEditAdress)
                {
                    if (m_lastClick != lineEditAdress){
                        m_lastClick = lineEditAdress;
                        ui->lineEditAdress->selectAll();
                        return true;
                    }else if (m_lastClick == lineEditAdress)
                    {
                        goto flag;
                    }
                }
                if (target == ui->textEditRemark)
                {
                    if (m_lastClick != textEditRemark){
                        m_lastClick = textEditRemark;
                        ui->textEditRemark->selectAll();
                        return true;
                    }else if (m_lastClick == textEditRemark)
                    {
                        goto flag;
                    }
                }
                if (target == ui->pushButtonSave)
                {
                    m_lastClick = pushButtonSave;
                    goto flag;
                }
            }
        }

        //键盘事件
        if (event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if (keyEvent->key() == Qt::Key_Escape)
            {
                this->hide();
                return true;
            }
            if ((keyEvent->key() == Qt::Key_Enter) || (keyEvent->key() == Qt::Key_Return))
            {
                if (target == ui->lineEditMembercardID)
                {
                    ui->lineEditMemberName->setFocus();
                    ui->lineEditMemberName->selectAll();
                    return true;
                }
                if (target == ui->lineEditMemberName)
                {
                    ui->comboBoxGender->setFocus();
                    ui->comboBoxGender->showPopup();
                    return true;
                }
                if (target == ui->comboBoxGender)
                {
                    ui->lineEditTel->setFocus();
                    ui->lineEditTel->selectAll();
                    return true;
                }
                if (target == ui->lineEditTel)
                {
                    ui->lineEditEmail->setFocus();
                    ui->lineEditEmail->selectAll();
                    return true;
                }
                if (target == ui->lineEditEmail)
                {
                    ui->lineEditAdress->setFocus();
                    ui->lineEditAdress->selectAll();
                    return true;
                }
                if (target == ui->lineEditAdress)
                {
                    ui->textEditRemark->setFocus();
                    ui->textEditRemark->selectAll();
                    return true;
                }
                if (target == ui->textEditRemark)
                {
                    ui->pushButtonSave->setFocus();
                    return true;
                }
                if (target == ui->pushButtonSave)
                {
                    on_pushButtonSave_clicked();
                    return true;
                }
            }
        }
    }
flag:
    return QWidget::eventFilter(target, event);
}

void ModifyMemberInformation::on_pushButtonCancel_clicked()
{
    this->hide();
}

void ModifyMemberInformation::on_pushButtonSave_clicked()
{
    m_personMember.set_personName(ui->lineEditMemberName->text());
    m_personMember.set_personGender(ui->comboBoxGender->currentText());
    m_personMember.set_personTel(ui->lineEditTel->text());
    m_personMember.set_personEmail(ui->lineEditEmail->text());
    m_personMember.set_personAdress(ui->lineEditAdress->text());
    m_personMember.set_memberRemark(ui->textEditRemark->toPlainText());
    if (!m_sql->UpdatMemberInformation(m_personMember))
    {
        QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                             tr("\n修改会员信息失败!"));
    }
    else
    {
        emit updateMemberProfile();
        this->hide();
    }
}
